package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.storage.StorageService;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminAdController_list;
import com.neu.doc.AdminStorageController_ResOK;
import com.neu.doc.AdminStorageController_list;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.domain.ShoppingStorage;
import com.neu.service.*;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/storage")
@Validated
public class AdminStorageController {
    private final Log logger = LogFactory.getLog(AdminStorageController.class);

    @Autowired
    private StorageService storageService;
    @Autowired
    private ShoppingStorageService ShoppingStorageService;

    private Object validate(ShoppingStorage ss) {
        String name = ss.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        String content = ss.getKey();
        if (StringUtils.isEmpty(content)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }
//    @RequiresPermissions("admin:storage:list")
//    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "查询")
//    TODO
    @ApiOperation(value="获取存储(List)分页数据", notes="分页查询用户存储信息",response = AdminStorageController_list.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "键名", required = true, dataType = "string"),
            @ApiImplicitParam(name = "name", value = "对象名字", required = true, dataType = "string"),
            @ApiImplicitParam(name = "page", value = "数据总页数", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "limit", value = "排序规则", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "string"),
            @ApiImplicitParam(name = "order", value = "排序规则", required = false, dataType = "string")
    })
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "查询")
    @GetMapping("/list")
    public Object querySelective(String name, String key,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "add_time") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) {
    List<ShoppingStorage> ss =ShoppingStorageService.querySelective(key, name, page, limit, sort, order);
    return ResponseUtil.okList(ss);
    }
    

//    @RequiresPermissions("admin:storage:create")
//    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "上传")
//  TODO
    @ApiOperation(value="上传存储", notes="上传对象存储", response = AdminStorageController_ResOK.class)
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "上传")
    @PostMapping("/create")
    public Object create(@ApiParam(name="Shoppingstorage對象",value="传入json格式",required=true)
	@RequestBody ShoppingStorage ss) {
    	ShoppingStorageService.add(ss);
    	return ResponseUtil.ok(ss);
    }
    

//    @RequiresPermissions("admin:storage:update")
//    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "编辑")
//  TODO
    @ApiOperation(value="编辑仓库", notes="编辑一条仓库信息", response = AdminAdController_ResOK.class)
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "编辑")
    @PostMapping("update")
    public Object update(@ApiParam(name="Shoppingstorage對象",value="传入json格式",required=true)
	@RequestBody ShoppingStorage ss) {
Object error = validate(ss);
if (error != null) {
    return error;
}
if (ShoppingStorageService.update(ss) == 0) {
    return ResponseUtil.updatedDataFailed();
}

return ResponseUtil.ok(ss);
}

//    @RequiresPermissions("admin:storage:delete")
//    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "删除")
//  TODO
    @ApiOperation(value="删除仓库", notes="删除一条仓库信息", response = ResponseOK.class)
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
    		@RequestBody ShoppingStorage ss) {
        String key = ss.getKey();
        if (key == null) {
            return ResponseUtil.badArgument();
        }
        ShoppingStorageService.deleteByKey(key);
        return ResponseUtil.ok();
    }
}
